<?php

class Model_Tag extends Model_Database
{
	var $table_tag = 'tag';
	var $table_tagmap = 'tagmap';
	
	public function get_list($offset = 0, $limit = 20){
		$sql = "select id, name, count from {$this->table_tag} where del=0 order by id desc ";
		$sql .= "limit {$offset}, {$limit}";
		$query = DB::query(Database::SELECT, $sql);
		return $query->execute()->as_array();
	}
	
	public function get_name_by_tagid($tagid){
		$sql = "select id, name, count from {$this->table_tag} where id=:id and del=0";
		$query = DB::query(Database::SELECT, $sql);
		$query->param(':id', $tagid);
		return $query->execute()->as_array();
	}
	
	public function get_tagid_by_aid($aid){
		$sql = "select tagid from {$this->table_tagmap} where del=0 and aid=:aid";
		$query = DB::query(Database::SELECT, $sql);
		$query->parameters(array(
			':aid' => $aid,
		));
		$re = $query->execute()->as_array();
		$tagids = array();
		foreach ($re as $per){
			$tagids[] = $per['tagid'];
		}
		return $tagids;
	}
	
	public function get_tags_by_aid($aid){
		$sql = "select t.id, t.name, t.count from {$this->table_tagmap} as m ";
		$sql .= "left join {$this->table_tag} as t on t.id = m.tagid ";
		$sql .= "where m.del=0 and t.del=0 and m.aid=:aid";
		$query = DB::query(Database::SELECT, $sql);
		$query->parameters(array(
			':aid' => $aid,
		));
		return $query->execute()->as_array();
	}
	
	public function get_tagid($tagname_arr){
		$tags = array();
		$sql = "select id, name from {$this->table_tag} where del=0 and name=:tagname";
		$query = DB::query(Database::SELECT, $sql)
			->bind(':tagname', $tagname);
		foreach ($tagname_arr as $tagname){
			$re = $query->execute();
			$tagid = isset($re[0]['id']) ? $re[0]['id'] : -1;
			$tags[] = array('tag' => $tagname, 'id' => $tagid);
		}
		return $tags;
	}
	
	public function add($tag_arr){
		$id_arr = array();
		$sql = "insert into {$this->table_tag} (name) values(:tag)";
		$query = DB::query(Database::INSERT, $sql)
			->bind(':tag', $tag);
		foreach ($tag_arr as $tag){
			$tag = addslashes(trim($tag));
			list($id) = $query->execute();
			$id_arr[] = $id;
		}
		return $id_arr;
	}
	
	public function add_map($aid, $tagid_arr){
		$sql = "insert into {$this->table_tagmap} (aid, tagid) values(:aid, :tagid)";
		$query = DB::query(Database::INSERT, $sql)
			->bind(':aid', $aid)
			->bind(':tagid', $tagid);
		foreach ($tagid_arr as $tagid){
			list($noid) = $query->execute();
		}
		return true;
	}
	
	public function del($ids){
		$rows = 0;
		$sql = "update {$this->table_tag} set del=1 where id=:id";
		$query = DB::query(Database::UPDATE, $sql)
			->bind(':id', $id);
		foreach ($ids as $id){
			$rows += $query->execute();
		}
		if (count($ids) == $rows){
			return true;
		}else {
			return false;
		}
	}
		
	public function del_map($aid, $tagid_arr){
		$rows = 0;
		$sql = "update {$this->table_tagmap} set del=1 where aid=:aid and tagid=:tagid";
		$query = DB::query(Database::UPDATE, $sql)
			->bind(':aid', $aid)
			->bind(':tagid', $tagid);
		foreach ($tagid_arr as $tagid){
			$rows += $query->execute();
		}
		if (count($tagid_arr) == $rows){
			return true;
		}else {
			return false;
		}
	}
	
	public function up_count($tagids, $add = 1){
		$rows = 0;
		if ($add == 1){
			$sql = "update {$this->table_tag} set count=count+1 where id=:tagid";
		}else {
			$sql = "update {$this->table_tag} set count=count-1 where id=:tagid";
		}
		$query = DB::query(Database::UPDATE, $sql)
			->bind(':tagid', $tagid);
		foreach ($tagids as $tagid){
			$rows += $query->execute();
		}
		if (count($tagids) == $rows){
			return true;
		}else {
			return false;
		}
	}
	
	public function hot_tag($num = 10){
		$sql = "select id, name, count from {$this->table_tag} where del=0 order by count desc limit :num";
		$query = DB::query(Database::SELECT, $sql);
		$query->parameters(array(
			':num' => $num,
		));
		$tags = $query->execute()->as_array();
		
		if (!empty($tags)){
			// 计算等级 rank 1 ~ 4
			$last_key = count($tags) - 1;
			$max = $tags[0]['count'];
			$min = $tags[$last_key]['count'];
			$step = ceil(($max - $min) / 4);
			$rank1 = $step * 1;
			$rank2 = $step * 2;
			$rank3 = $step * 3;
			$rank4 = $step * 4;
			$tids = array();
			foreach ($tags as &$tag){
				$tids[] = $tag['id'];
				if ($tag['count'] >= 0 && $tag['count'] <= $rank1){
					$tag['rank'] = 4;
				}
				if ($tag['count'] >= $rank1 && $tag['count'] <= $rank2){
					$tag['rank'] = 3;
				}
				if ($tag['count'] >= $rank2 && $tag['count'] <= $rank3){
					$tag['rank'] = 2;
				}
				if ($tag['count'] >= $rank3 && $tag['count'] <= $rank4){
					$tag['rank'] = 1;
				}
			}
			array_multisort($tids, $tags);
		}
		return $tags;
	}
	
}
